﻿using SuperFrame.Charts.Points;
using System.Windows;
using System.Windows.Media;
using System.Linq;

namespace SuperFrame.Charts
{
    /// <summary>
    /// 折线图
    /// </summary>
    public class SeriesLine : BaseLineSeries
    {
        override public void GenerateDatas()
        {
            LinePoints.Clear();
            if (!isPointsGenerated)
                Parts.Clear();
            if (this.Points != null && this.seriesContainer != null)
            {
                CalculateMinAndMax();
                IntializePoints();
                foreach (ChartPoint point in this.Points)
                {
                    Point linePoint = NormalizePoint(new Point(point.XValue, point.YValue));
                    LinePoints.Add(linePoint);
                }
                if (this.RenderingMode == RenderingMode.Default)
                {
                    if (!isPointsGenerated)
                    {
                        for (int i = 0; i < this.LinePoints.Count - 1; i++)
                        {
                            PartLine PartLine = new PartLine(this.LinePoints[i], this.LinePoints[i + 1], this.Points[i].YValue+"", this.Points[i + 1].YValue+"");
                            SetBindingForStrokeandStrokeThickness(PartLine);
                            this.Parts.Add(PartLine);
                        }
                        isPointsGenerated = true;
                    }
                    else
                    {
                        int i = 0;
                        foreach (PartLine part in this.Parts)
                        {
                            part.X1 = this.LinePoints[i].X;
                            part.Y1 = this.LinePoints[i].Y;
                            part.X2 = this.LinePoints[i + 1].X;
                            part.Y2 = this.LinePoints[i + 1].Y;
                            part.Refresh();
                            i++;
                        }
                    }
                }
                this.seriesContainer.Invalidate();
            }

            isRefreshed = false;
        }

        public SeriesLine()
        {
            LinePoints = new PointCollection();
        }

        internal override SeriesContainer CreateContainer()
        {
            return new ContainerLine();
        }

        public PointCollection LinePoints
        {
            get { return (PointCollection)GetValue(LinePointsProperty); }
            set { SetValue(LinePointsProperty, value); }
        }
        public static readonly DependencyProperty LinePointsProperty = DependencyProperty.Register("LinePoints", typeof(PointCollection), typeof(SeriesLine), new PropertyMetadata(null));

    }

}
